package secondriver.springsubway.demo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Spring ApplicationContext生命周期
 * 
 * ApplicationContext是在应用启动的时候加载所有单例Bean,getBean时直接返回
 *
 */
public class ApplicationContextLifecycle implements 
		BeanNameAware, BeanFactoryAware, ApplicationContextAware,
		InitializingBean, DisposableBean {

	private String name;

	public ApplicationContextLifecycle() {
		System.out.println("ApplicationContextLifecycle[constructor]");
	}

	public void setName(String name) {
		this.name = name;
		System.out
				.println("ApplicationContextLifecycle[setName] populate properties.");
	}

	public String getName() {
		System.out
				.println("ApplicationContextLifecycle[getName] get properties.");
		return name;
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("ApplicationContextLifecycle[setBeanName] " + name);
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("ApplicationContextLifecycle[setBeanFactory] "
				+ beanFactory);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		System.out.println("ApplicationContextLifecycle[setApplicationContex] "
				+ applicationContext);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("ApplicationContextLifecycle[afterPropertiesSet]");
	}

	//Bean destroy-method attribute
	public void init() {
		System.out
				.println("ApplicationContextLifecycle[init] coustom init method called.");
	}


	@Override
	public void destroy() throws Exception {
		System.out.println("ApplicationContextLifecycle[destroy]");
	}

	public void close() {
		System.out
				.println("ApplicationContextLifecycle[close] customer close method called.");
	}

}
